package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Paymentplan;

@Repository(value = "paymentPlanDao")
@Transactional(readOnly = false)
public class PaymentPlanDaoImpl implements PaymentPlanDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	public List<Paymentplan> getListPaymentPlan() {
		TypedQuery<Paymentplan> q = em.createNamedQuery("Paymentplan.findAll",Paymentplan.class);
    	return q.getResultList();
	}
	public List<Paymentplan> getListPaymentPlanByAccountsPayableId(Integer accountsPayableId) {
		TypedQuery<Paymentplan> q = em.createNamedQuery("Paymentplan.findByAccountsPayableId",Paymentplan.class);
		q.setParameter("accountsPayableId",accountsPayableId);
    	return q.getResultList();
	}

	public List<Paymentplan> getListPaymentPlanByAccountReceivableId(Integer accountReceivableId) {
		TypedQuery<Paymentplan> q = em.createNamedQuery("Paymentplan.findByAccountReceivableId",Paymentplan.class);
		q.setParameter("accountReceivableId",accountReceivableId);
    	return q.getResultList();
	}

	public void remove(Paymentplan paymentPlan) {
		em.remove(em.getReference(Paymentplan.class,paymentPlan.getAccountReceivableId()));
	}

	public void update(Paymentplan paymentPlan) {
		em.merge(paymentPlan);
	}

	public void add(Paymentplan paymentPlan) {
		em.persist(paymentPlan);
	}

	public Paymentplan findPaymentPlanByPaymentPlanId(short paymentPlanId) {
		Paymentplan res = null;
		try {
			res=em.find(Paymentplan.class,paymentPlanId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}

}
